﻿bingo.using('service/system/mainService');
bingo.module('system', function() {
	bingo.controller('mains', function() {
		bingo.action('welcome', function($view, mainService, $param, $location) {
			$view.$getNode().show();
			$view.systemInfo = mainService.getSystemInfo();
			if($view.systemInfo.code == 0) {
				//已登录
				$location.href('view/system/mains/main');
			} else {
				//未登录
				$location.href('view/system/mains/login');
			}
		});
		bingo.action('main', function($view, mainService, $param, $location, $cache, $timeout) {
			$view.$getNode().show();
			var systemInfo = mainService.getSystemInfo();
			$view.user = systemInfo.data.user;
			$view.appCss = {
				href:"lib/css/appdefault.css"//无左侧菜单时候加载这个
			};
			$view.menuHidden = {
				bingoSided:{
					class: "layui-side layui-bg-black bingo-side"
				},
				bodySide:{
					class: "layui-body"
				}
			};
			$view.skinHref = {
				href:"lib/css/themes/default.css"
			};
			$view.skinShow = {
				class:"layui-nav-item"
			};
			$view.bodyContent = {
				title:"",
				icon:""
			};
			if($view.user.leftNavigation.length>0){
				//有左侧菜单时候加载
				$view.appCss.href = "lib/css/app.css";
			}else{
				$view.menuHidden.bingoSided.class = "layui-side layui-bg-black bingo-side bingo-sided";
				$view.menuHidden.bodySide.class = "layui-body bingo-body-folded";
			}
			/*$location.onChange(function(url){
			    console.log("onChange:"+url);
			});
			
			$location.onLoaded(function(url){
			    console.log("onLoaded:"+url);
			});*/
			//跳转
			$view.gotoContent = function(item) {
				$view.bodyContent.title = item.name;
				$view.bodyContent.icon = item.icon
				$location.href(item.href,item.target);
			};
			//皮肤切换
			$view.switchSkin = function(item){
				$view.skinHref.href = item.src;
			};
			//左侧菜单显示隐藏
			$view.changeMenuHidden = function() {
				if($view.menuHidden.bingoSided.class == "layui-side layui-bg-black bingo-side") {
					$view.menuHidden.bingoSided.class = "layui-side layui-bg-black bingo-side bingo-sided";
					$view.menuHidden.bodySide.class = "layui-body bingo-body-folded";
				} else {
					$view.menuHidden.bingoSided.class = "layui-side layui-bg-black bingo-side";
					$view.menuHidden.bodySide.class = "layui-body";
				}
			};
			//退出登录
			$view.loginOut = function() {
				var res = mainService.exit();
				if(res.code == 0) {
					//登出
					$location.href('view/system/mains/login','main');
				} else {
					console.log(res.msg);
				}
			};
			$view.onReady(function(){
				//加载js资源
				bingo.using('lib/layui/layui.js',function(){
					layui.use(['element'], function() {
						//下拉菜单需要加载element.js
						element = layui.element;
						element.init();
						//跳转默认首页
						$view.gotoContent($view.user.role.homePage);
						$view.$update();
					});
				});
			});
		});
		bingo.action('login', function($view, mainService, $param, $location) {
			$view.$getNode().show();
			//给默认值
			$view.user = {
				userName: "admin",
				password: "admin"
			};
			$view.login = function() {
				var res = mainService.signin($view.user);
				if(res.code == 0) {
					//登录
					$location.href('view/system/mains/main','main');
				} else {
					console.log(res.msg);
				}
			};
			$view.onReady(function(){
				//加载js资源
				bingo.using('lib/sideshow/js/TweenLite.min.js');
				bingo.using('lib/sideshow/js/EasePack.min.js');
				bingo.using('lib/sideshow/js/rAF.js');
				bingo.using('lib/sideshow/js/demo-1.js',function(){
					initLogin();
				});
				bingo.using('lib/layui/layui.js');
			});
		});
		
		bingo.action('list', function($view, mainService, $param, $location) {
			$view.$getNode().show();
		});
	});

});